<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/7
 * Time: 18:24
 */

namespace Admin\Controller;


class QyController extends AdminController
{
    public function _myinitialize(){
        $tb_name=strtolower(CONTROLLER_NAME);
        $this->db=M($tb_name);
    }

    public function lst(){

        $where['status']=1;
        C('LIST_ROWS',100);
        $lists=$this->lists($this->db,$where,"updatetime desc");
        $this->assign('lists',$lists);

        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data=$this->db->create();
            if($data === false){
                $this->error($this->db->getError());
            }else{
                $data['updatetime']=time();
            }
            $rid=$this->db->add($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }
            $this->redirect('lst');
        }
    }

    public function edit(){
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime'] = time();
                $rid = $this->db->save($data);

                if ($rid === false) {
                    $this->error($this->db->getDbError());
                }
                $this->redirect('lst');
            }
        } else {
            $id = I('id');
            if (!empty($id)) {
                $data = $this->db->where(array("id" => $id))->find();
                $this->ajaxReturn($data);
            }
        }
    }

    public function del(){
        $id = I('id');
        if (!empty($id)) {
            $data['status'] = 0;
            $data['updatetime'] = time();
            $this->db->where(array('id' => $id))->setField($data);
            $this->redirect('lst');
        }
    }
}